package com.JobFind.logic.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.JobFind.DAO.IDeliverDAO;
import com.JobFind.DAO.IRecruitmentDAO;
import com.JobFind.DAO.IUserDAO;
import com.JobFind.DTO.DeliverBriefDTO;
import com.JobFind.DTO.DeliverDTO;
import com.JobFind.DTO.DeliverShowDTO;
import com.JobFind.logic.IDeliverLogic;
import com.JobFind.logic.IRecruitmentLogic;
import com.JobFind.logic.IResumeLogic;
import com.JobFind.model.TbDeliver;

@Component
public class DeliverLogic implements IDeliverLogic{
	private IResumeLogic resumeLogic;
	@Resource
	private IRecruitmentLogic recruitmentLogic;
	
	@Resource
	private IRecruitmentDAO recruitmentDAO;
	
	@Resource
	private IDeliverDAO deliverDAO;
	
	@Resource
	private IUserDAO userDAO;
	
	public IResumeLogic getResumeLogic() {
		return resumeLogic;
	}
	
	@Resource
	public void setResumeLogic(IResumeLogic resumeLogic) {
		this.resumeLogic = resumeLogic;
	}


	@Override
	@Transactional
	public Integer adDeliver(Integer recruitmentId,Integer userId) throws Exception {
		Integer result = 0;
		try {
			Integer resumeId = this.resumeLogic.getResumeIdByUserId(userId);
			DeliverDTO deliverDTO = new DeliverDTO();
			deliverDTO.setRecruitmentId(recruitmentId);
			deliverDTO.setResumeCopyId(resumeId);
			deliverDTO.setUserId(userId);
			deliverDTO.setDeliverStatus(0);
			result = this.deliverDAO.adDeliver(deliverDTO.toDAO());
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getResumeList(Integer companyId) throws Exception {
		String result = "";
		List<DeliverShowDTO> deliverShowDTOList = new ArrayList<DeliverShowDTO>();
		List<TbDeliver> deliverList = null;
		try {
				deliverList = this.deliverDAO.getResumeList(companyId);
				if(deliverList == null || deliverList.size() == 0)
					result = "";
				else{
					for(TbDeliver deliver :deliverList){
						deliverShowDTOList.add(deliver.toDeliverShowDTO());
					}
					result = JSONArray.fromObject(deliverShowDTOList).toString();
				}
				
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}
	
	@Override
	@Transactional
	public String getPassRersonList(Integer companyId) throws Exception {
		String result = "";
		List<DeliverShowDTO> deliverShowDTOList = new ArrayList<DeliverShowDTO>();
		List<TbDeliver> deliverList = null;
		try {
				deliverList = this.deliverDAO.getPassRersonList(companyId);
				if(deliverList == null || deliverList.size() == 0)
					result = "";
				else{
					for(TbDeliver deliver :deliverList){
						deliverShowDTOList.add(deliver.toDeliverShowDTO());
					}
					result = JSONArray.fromObject(deliverShowDTOList).toString();
				}
				
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}
	
	@Override
	@Transactional
	public Integer upDeliver(Integer userId,Integer deliverId, Integer deliverStatus, Integer recruitmentId)
			throws Exception {
		Integer result = 0;
		try {
			if(deliverStatus == 1){
				this.recruitmentLogic.upRecritmentAreNum(recruitmentId);
				Integer companyId = this.recruitmentDAO.getCompanyIdByRecId(recruitmentId);
				this.userDAO.updateUserCom(userId, companyId);
			}
			result = this.deliverDAO.upDeliver(deliverId, deliverStatus);

		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getResumeResult(Integer userId) throws Exception {
		String result = "";
		List<DeliverBriefDTO> deliverBriefDTOList = new ArrayList<DeliverBriefDTO>();
		List<TbDeliver> deliverList = null;
		try {
				deliverList = this.deliverDAO.getResumeResult(userId);
				if(deliverList == null || deliverList.size() == 0)
					result = "";
				else{
					for(TbDeliver deliver :deliverList){
						deliverBriefDTOList.add(deliver.toBriefDTO());
					}
					for(int i=0;i<deliverBriefDTOList.size();i++){
						String companyName=this.recruitmentDAO.getCompanyNameByRecId(deliverBriefDTOList.get(i).getRecruitmentId());
						deliverBriefDTOList.get(i).setCompanyName(companyName);
					}
					result = JSONArray.fromObject(deliverBriefDTOList).toString();
				}
				
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}

}
